import axios from 'axios'
import store from '@/store'
import router from '@/router'
import { Message } from 'element-ui'
const instance = axios.create({
  baseURL: 'http://interview-api-t.itheima.net'

})

instance.interceptors.request.use(function (config) {
  config.headers.Authorization = 'Bearer ' + store.state.token
  return config
}, function (error) {
  return Promise.reject(error)
})

instance.interceptors.response.use(function (response) {
  if (response.data.code !== 10000) {
    throw new Error('code!=10000')
  }
  return response.data
}, function (error) {
  if (error.response && error.response.status === 401) {
    router.push('/login?redirect=' + router.currentRoute.fullPath)
    Message.error('token失效,请重新登录')
  }
  return Promise.reject(error)
})

export default instance
